<template>
  <el-dialog
    title="查看客户留言"
    v-model="visible"
    top="10vh"
    width="600px"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form
      ref="dataFormRef"
      :model="dataForm"
      label-position="right"
      label-width="120px"
      :disabled="true"
      v-loading="loading"
    >
      <el-form-item label="用户姓名" prop="name">
        <el-input v-model="dataForm.name"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="dataForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="dataForm.email"></el-input>
      </el-form-item>
      <el-form-item label="留言Ip" prop="messageIp">
        <el-input v-model="dataForm.messageIp"></el-input>
      </el-form-item>
      <el-form-item label="留言地区" prop="address">
        <el-input v-model="dataForm.address"></el-input>
      </el-form-item>
      <el-form-item label="留言来源" prop="messageUrl">
        <el-input v-model="dataForm.messageUrl"></el-input>
      </el-form-item>
      <el-form-item label="留言内容名称" prop="contentName">
        <el-input v-model="dataForm.contentName"></el-input>
      </el-form-item>
      <el-form-item label="留言内容" prop="content">
        <el-input type="textarea" v-model="dataForm.content"></el-input>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button type="primary" @click="close">关闭</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref, watch, type PropType } from 'vue'
import { UPDATE_MODEL_EVENT } from 'cm/contants'

import { validPhone } from '@/utils/validate'

import { getMessageInfo, updateStatus, type ICreateOrUpdateMessage } from '@/api/message'
import { ElMessage } from 'element-plus'

const props = defineProps({
  modelValue: {
    type: Boolean,
    required: true,
    default: false
  },
  currId: {
    type: String,
    required: true,
    default: ''
  },
})

const emit = defineEmits([UPDATE_MODEL_EVENT, 'change'])

const visible = ref<boolean>(false)

const handleClose = () => {
  emit(UPDATE_MODEL_EVENT, false)
}

const defaultForm = {
  id: '',
  name: "",
  phone: "",
  email: "",
  messageIp: "",
  address: "",
  messageUrl: "",
  contentName: "",
  contentId: "",
  content: ""
}
// 表单逻辑
const dataForm = ref<ICreateOrUpdateMessage>({ ...defaultForm });

const loading = ref<boolean>(false)

// 查询留言详情
const getMessageInfoApi = async (currId: string) => {
  loading.value = true
  const res = await getMessageInfo(currId)
  loading.value = false
  if (res?.code === 200) {
    dataForm.value = res.data as ICreateOrUpdateMessage;
    if (dataForm.value.status === 0) {
      UpdateMessageStatusApi(currId);
    }
  }
}

// 更改留言状态
const UpdateMessageStatusApi = async (currId: string) => {
  loading.value = true
  const res = await updateStatus({ id: currId, status: 1 })
  loading.value = false
  if (res?.code === 200) {
    console.log('修改阅读成功')
  }
}

const close = () => {
  emit('change')
  handleClose()
}

watch(
  () => props.modelValue,
  async () => {
    visible.value = props.modelValue
    if (props.modelValue && props.currId) {
      await getMessageInfoApi(props.currId)
    }
  }
)

</script>
